package is.glif.fenius.rrm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONObject;

import org.apache.log4j.Logger;
import org.ho.yaml.Yaml;

public class ConfigHolder {
    static private boolean DEBUG = true;
    static private Hashtable<String, String> alias = null;
    static private JSONArray fibre = null;

    private ConfigHolder () {}

    static private void readAlias(String filename) 
    {
	try {
	    alias = new Hashtable<String, String>();

            String jsonString = org.apache.cxf.helpers.FileUtils.getStringFromFile
                (new java.io.File(filename));
            JSONArray a = new JSONArray(jsonString);

	    for (int i = 0; i < a.length(); i ++) {
		JSONArray aa = a.getJSONArray(i);
		if (aa == null) continue;
		
		for (int ii = 0; ii < aa.length(); ii ++) {
		    JSONObject o = aa.getJSONObject(ii);

		    String urn = o.getString("urn");
		    String rrm = o.getString("rrm");

		    if (DEBUG) System.err.println("=====" + urn + "//" + rrm);
		    if ((urn != null) && (rrm != null)) alias.put(urn, rrm);
		}
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(1);
	}
    }

    static private void readFibre(String filename) 
    {
	try {
            String jsonString = org.apache.cxf.helpers.FileUtils.getStringFromFile
                (new java.io.File(filename));
            fibre = new JSONArray(jsonString);

	    for (int i = 0; i < fibre.length(); i ++) {
		JSONObject o = fibre.getJSONObject(i);
		if (o == null) continue;

		String ap = o.getString("a");
		String zp = o.getString("z");

		if (DEBUG) System.err.println("=====" + ap + "//" + zp);
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(1);
	}
    }

    static public Hashtable<String, String> getAlias()
    {
	if (alias == null) readAlias("config/alias.json");
	return alias;
    }

    static public JSONArray getFibre()
    {
	if (fibre == null) readFibre("config/fibre.json");
	return fibre;
    }
}
